<template>
  <el-dialog :visible="showVisible" center :show-close="false" width="920px">
    <div class="b g3 f16 tl" slot="title">
      <div class="ks-row-between pl15 pr15">
        <span>{{ isNew ? $t("sys.new_environment_variable") : $t("sys.edit_environment_variable") }}</span>
        <span class="el-icon-close g9 f26 poi" @click="handleCancel"></span>
      </div>
    </div>

    <el-form class="pl30 pr30" :model="formData" ref="form" label-width="100px">
      <el-form-item
        :label="$t('sys.variable_name')"
        prop="varName"
        :rules="[
          { required: true, message: $t('sys.enter_variable_name'), trigger: 'change' },
          {
            pattern: COMMON_CODE_64_I_REG,
            message: COMMON_CODE_64_I_REG_MSG,
            trigger: 'change',
          },
        ]"
      >
        <el-input
          v-model="formData.varName"
          style="width: 100%"
          :disabled="!isNew"
          class="mr10"
          filterable
          allow-create
          :loading="typeLoading"
          :placeholder="$t('sys.enter_variable_name')"
          :maxlength="64"
          show-word-limit
        >
        </el-input>
      </el-form-item>

      <el-form-item :label="$t('sys.remarks')" prop="notes">
        <el-input
          v-model="formData.notes"
          type="textarea"
          style="width: 100%"
          :placeholder="$t('sys.enter_remarks')"
          :maxlength="120"
          show-word-limit
        />
      </el-form-item>
      <el-divider content-position="left">{{$t('sys.value')}}</el-divider>
      <el-form-item
        v-for="(item, index) in formData.systemVariableValues"
        :label="item.envName"
        :key="item.envKey"
        :prop="'systemVariableValues.' + index + '.varValue'"
      >
        <el-input
          v-model="item.varValue"
          style="width: 100%"
          :placeholder="$t('sys.enter_value')"
        ></el-input>
      </el-form-item>
    </el-form>

    <div slot="footer" class="tr">
      <el-button @click="handleCancel">{{$t('sys.cancel')}}</el-button>
      <el-button
        :loading="confirmLoading"
        type="primary"
        @click="submit"
      >{{$t('sys.confirm')}}</el-button
      >
    </div>
  </el-dialog>
</template>

<script>
import { COMMON_CODE_64_I_REG, COMMON_CODE_64_I_REG_MSG } from '@/utils/reg'

import { getHmenvironmentList } from '@/apis/paas/HmEnvironmentController.js'
import { postHmsystemvariable,putHmsystemvariable } from '@/apis/paas/HmSystemVariableController.js'
export default {
  name: 'SystemForm',
  data () {
    return {
      showVisible: false,
      confirmLoading: false,
      envList: [],
      formData: {
        varKey: '',
        systemVariableValues: [
          {
            envKey: '',
            envName: '',
            varValue: ''
          }
        ],
        varName: '',
        notes: ''
      },
      COMMON_CODE_64_I_REG,
      COMMON_CODE_64_I_REG_MSG,
      typeLoading: false
    }
  },

  computed: {
    isNew () {
      return !this.formData.varKey
    }
  },

  created () {},

  methods: {
    async addForm (record = {}) {
      this.envList = await getHmenvironmentList()
      const data = this.envList
      const envList = []
      for (let i = 0; i < data.length; i++) {
        const d = data[i]
        if (d.hidden !== 1) {
          envList.push(d)
        }
      }
      this.envList = envList
      this.showVisible = true
      this.formData = {
        ...record
      }
      const list = this.envList.map((item) => {
        return {
          envKey: item.key,
          envName: item.name,
          varValue: ''
        }
      })
      this.$set(this.formData, 'systemVariableValues', [...list])
      this.$nextTick(() => {
        this.$refs.form.clearValidate()
      })
    },

    async editForm (record = {}) {
      this.showVisible = true
      this.formData = {
        ...record
      }
      const recordList = record.systemVariableValues || []
      this.envList = await getHmenvironmentList()
      const data = this.envList
      const envList = []
      for (let i = 0; i < data.length; i++) {
        const d = data[i]
        if (d.hidden !== 1) {
          envList.push(d)
        }
      }
      this.envList = envList
      const list = this.envList.map((item) => {
        const res = recordList.find((value) => {
          return value.envKey === item.key
        })
        return {
          envKey: item.key,
          envName: item.name,
          varValue: res?.varValue
        }
      })
      this.$set(this.formData, 'systemVariableValues', [...list])
      this.$nextTick(() => {
        this.$refs.form.clearValidate()
      })
    },

    async submit () {
      try {
        await this.$refs.form.validate()
        const { notes, varName, systemVariableValues, varKey } = this.formData
        let params = {}
        let request = null
        if (this.isNew) {
          params = { varName, notes, systemVariableValues }
          request = postHmsystemvariable
        } else {
          params = {
            varKey,
            notes: notes,
            varName,
            systemVariableValues
          }
          request = putHmsystemvariable
        }

        for (const key in params) {
          params[key] =
            typeof params[key] === 'string' ? params[key].trim() : params[key]
        }
        request(params).then((res) => {
          this.handleCancel()
          this.$emit('ok')
        })
      } catch (error) {
        console.log(error)
      }
    },
    handleCancel () {
      this.showVisible = false
    }
  }
}
</script>

<style lang="scss" scoped></style>
